# player.gd
extends Node
 
@export var _character: CharacterBody3D # 可移动的角色
@export var _camera: Camera3D # 跟随角色的相机
var _input_direction:Vector2
var _move_direction:Vector3

# 监听输入动作
func _input(event: InputEvent) -> void:
	# 按下对应热键
	if event.is_action_pressed("run"):
		_character.run()
	# 松开了对应热键
	elif event.is_action_released("run"):
		_character.walk()
		
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	# 获取键盘输入("move_left",'move_right','move_forward','move_backward' 顺序不能错了)
	_input_direction = Input.get_vector("move_left",'move_right','move_forward','move_backward')
	# 
	_move_direction = (_camera.basis.x * Vector3(1,0,1)).normalized() * _input_direction.x
	# 向后移动时, 相机向上移动
	_move_direction += (_camera.basis.z * Vector3(1,0,1)).normalized() * _input_direction.y
	
	# 移动, move方法在character.gd中定义, 用于改变direction(前进方向)
	_character.move(_move_direction)
